package com.spic.business.train.trainSign.service.impl;

import com.spic.business.train.trainSign.domain.TrainSign;
import com.spic.business.train.trainSign.dto.TrainSignDto;
import com.spic.business.train.trainSign.mapper.TrainSignMapper;
import com.spic.business.train.trainSign.service.ITrainSignService;
import com.spic.business.train.trainUser.service.ITrainUserService;
import com.spic.common.core.utils.UUIDUtil;
import com.spic.common.security.utils.cache.UserCacheUtils;
import com.spic.system.api.RemoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 培训签到Service业务层处理
 * 
 * @author spic
 * @date 2021-10-15
 */
@Service
public class TrainSignServiceImpl implements ITrainSignService 
{
    @Autowired
    private TrainSignMapper trainSignMapper;
    @Autowired
    private ITrainUserService trainUserService;
    @Autowired
    private RemoteUserService userService;
    /**
     * 查询培训签到
     * 
     * @param id 培训签到主键
     * @return 培训签到
     */
    @Override
    public TrainSign selectTrainSignById(String id)
    {
        return trainSignMapper.selectTrainSignById(id);
    }

    @Override
    public List<TrainSign> selectTrainSignByTrainInfoId(String id)
    {

        List<TrainSign> trainSigns=trainSignMapper.selectTrainSignByTrainInfoId(id);
        trainSigns.forEach(trainSign -> {
            try {
                trainSign.setUserName(UserCacheUtils.getValue(trainSign.getUserId()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return trainSigns;
    }
    /**
     * 查询培训签到列表
     * 
     * @param trainSignDto 培训签到
     * @return 培训签到
     */
    @Override
    public List<TrainSign> selectTrainSignList(TrainSignDto trainSignDto) {
        try {
            List<TrainSign>  trainSigns =  trainSignMapper.selectTrainSignList(trainSignDto);
            for (TrainSign trainSign : trainSigns) {
                trainSign.setUserName(UserCacheUtils.getValue(trainSign.getUserId()));
                trainSign.setIsSign("是");
            }
            //返回结果
            List<TrainSign> result = new ArrayList<>();
            if (0 == trainSignDto.getIsSign()) {//查询未签到
                Map<Long, TrainSign> trainSignsMap = trainSigns.stream().collect(Collectors.toMap(TrainSign::getUserId, trainSign -> trainSign));
                String[] ids = trainSignMapper.selectallTrainNumber(trainSignDto).split(",");
                for (String id : ids) {
                    Long idLong = Long.parseLong(id);
                    TrainSign sign = trainSignsMap.get(idLong);
                    if (null != sign){
                        continue;
                    }
                    TrainSign trainSign = new TrainSign();
                    trainSign.setId(null);
                    trainSign.setUserId(idLong);
                    trainSign.setUserName(UserCacheUtils.getValue(trainSign.getUserId()));
                    trainSign.setSignTime(null);
                    trainSign.setIsSign("否");
                    result.add(trainSign);
                }
            } else if (1 == trainSignDto.getIsSign()) {//查询已签到
                result.addAll(trainSigns);
            } else {//查询全部数据
                Map<Long, TrainSign> trainSignsMap = trainSigns.stream().collect(Collectors.toMap(TrainSign::getUserId, trainSign -> trainSign));
                String[] ids = trainSignMapper.selectallTrainNumber(trainSignDto).split(",");
                for (String id : ids) {
                    Long idLong = Long.parseLong(id);
                    TrainSign sign = trainSignsMap.get(idLong);
                    if (null != sign) {
                        result.add(sign);
                    } else {
                        TrainSign trainSign = new TrainSign();
                        trainSign.setId(null);
                        trainSign.setUserId(idLong);
                        trainSign.setUserName(UserCacheUtils.getValue(trainSign.getUserId()));
                        trainSign.setSignTime(null);
                        trainSign.setIsSign("否");
                        result.add(trainSign);
                    }
                }
            }
            return result;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 新增培训签到
     * 
     * @param trainSign 培训签到
     * @return 结果
     */
    @Override
    public int insertTrainSign(TrainSign trainSign)
    {
        if(trainUserService.selectTrainUserByTrainIdAndUserId(trainSign.getTrainId(),trainSign.getUserId())!=null){
            trainSign.setId(UUIDUtil.createUUID());
            return trainSignMapper.insertTrainSign(trainSign);
        }else{
            return 0;
        }

    }

    /**
     * 修改培训签到
     * 
     * @param trainSign 培训签到
     * @return 结果
     */
    @Override
    public int updateTrainSign(TrainSign trainSign)
    {

        return trainSignMapper.updateTrainSign(trainSign);
    }

    /**
     * 批量删除培训签到
     * 
     * @param ids 需要删除的培训签到主键
     * @return 结果
     */
    @Override
    public int deleteTrainSignByIds(String[] ids)
    {
        return trainSignMapper.deleteTrainSignByIds(ids);
    }

    /**
     * 删除培训签到信息
     * 
     * @param id 培训签到主键
     * @return 结果
     */
    @Override
    public int deleteTrainSignById(String id)
    {
        return trainSignMapper.deleteTrainSignById(id);
    }
}
